package com.huibo.shopping.product.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huibo.shopping.domain.ShopType;
import com.huibo.shopping.product.service.ShopTypeService;
import com.huibo.shopping.util.AjaxResult;
import com.huibo.shopping.util.PageQuery;
import com.huibo.shopping.util.StrUtils;

@RestController
@RequestMapping("/shopType")
public class ShopTypeController {

	@Autowired
	private ShopTypeService ss;
	
	@GetMapping("/selectShopType")
	private ArrayList<ShopType> selectShopType(String key) {
		/*IPage<ShopType> ipage = ss.page(new Page<ShopType>(pageQuery.getPage(),pageQuery.getRows()),
				new QueryWrapper<ShopType>().like("name", pageQuery.getKeyworlds())
				.orderByDesc("createTime"));
		return new PageResult<ShopType>(ipage.getTotal(),ipage.getRecords());*/
		
		
		
		

		// 查询所有数据列
		List<ShopType> listAll = ss.list(new QueryWrapper<ShopType>().like("name", key));
		// 创建前端接收数据对象列表
		ArrayList<ShopType> list1 = new ArrayList<ShopType>();
		// 遍历获取到的前端数据对象列表
		for (ShopType menuResultPo1 : listAll) {
			ArrayList<ShopType> treeChildren = menuResultPo1.getChildren();
			// 再次遍历resultList
			for (ShopType menuResultPo2 : listAll) {
				// 当pid等于id时候,将pid所在的对象存入同一个(当父级id等于菜单id的时候,说明这个对象是一个子节点)
				if (menuResultPo1.getId().equals(menuResultPo2.getPid())) {

					ShopType st1 = new ShopType();
					st1.setId(menuResultPo2.getId());
					st1.setName(menuResultPo2.getName());
					st1.setDescription(menuResultPo2.getDescription());
					st1.setPid(menuResultPo2.getPid());
					st1.setPath(menuResultPo2.getPath());
					st1.setLogo(menuResultPo2.getLogo());
					st1.setCreateTime(menuResultPo2.getCreateTime());
					st1.setUpdateTime(menuResultPo2.getUpdateTime());
					st1.setTotalCount(menuResultPo2.getTotalCount());
					st1.setLabel(menuResultPo2.getName());
					st1.setChildren(menuResultPo2.getChildren());
					// 存入父节点节点对象
					treeChildren.add(st1);
				}
			}
			// 设置children属性
			menuResultPo1.setChildren(treeChildren);
			// 判断是否是根节点
			if (menuResultPo1.getPid() == 0) {
				menuResultPo1.setLabel(menuResultPo1.getName());
				list1.add(menuResultPo1);
			}

		}
		return list1;
		
	}
	
	@PostMapping("/insertShopType")
	private AjaxResult insertShopType(@RequestBody ShopType ShopType){
		System.err.println(ShopType);
		try {
			if(ShopType.getId() == null) {
				//新增
				ShopType.setCreateTime(System.currentTimeMillis());
				ss.save(ShopType);
			}else {
				//修改
				ss.updateById(ShopType);
			}
			return AjaxResult.getInstance().setMsg("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.getInstance().setMsg("保存失败").setCode(500);
		}
	}
	
	@GetMapping("/deleteShopType/{ids}")
	private AjaxResult deleteShopType(@PathVariable("ids") String ids){
		try {
			
			List<Long> l = StrUtils.splitStr2LongArr(ids);
			ss.removeByIds(l);
			System.err.println(l);
			return AjaxResult.getInstance().setMsg("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.getInstance().setMsg("保存失败").setCode(500);
		}
	}
	
}
